<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="shortcut icon" type="image/png" href="icon.png">



	<style>
		body{
			word-wrap: break-word;
			background:#f5f5f5 center top no-repeat;
			background-size: auto 680px;
		}
		pre{
			white-space:pre-wrap;
		}
		a{
			text-decoration: none;
			color:#06c;
		}
		a:hover{
			color:#f00;
		}

		.main{
			max-width:700px;
			margin:0 auto;
			padding-bottom:80px
		}

		.mainBox{
			margin-top:12px;
			padding: 12px;
			border-radius: 6px;
			background: #fff;
			--border: 1px solid #f60;
			box-shadow: 2px 2px 3px #aaa;
		}


		.mainBtn{
			display: inline-block;
			cursor: pointer;
			border: none;
			border-radius: 3px;
			background: #f60;
			color:#fff;
			padding: 0 15px;
			margin:3px 20px 3px 0;
			line-height: 36px;
			height: 36px;
			overflow: hidden;
			vertical-align: middle;
		}
		.mainBtn:active{
			background: #f00;
		}

		.recwaveChoice{
			cursor: pointer;
			display:inline-block;
			vertical-align: bottom;
			border-right:1px solid #ccc;
			background:#ddd;
			line-height:28px;
			font-size:12px;
			color:#666;
			padding:0 5px;
		}
		.recwaveChoice:first-child{
			border-radius: 99px 0 0 99px;
		}
		.recwaveChoice:last-child{
			border-radius: 0 99px 99px 0;
			border-right:none;
		}
		.recwaveChoice.slc,.recwaveChoice:hover{
			background:#f60;
			color:#fff;
		}

		.lb{
			display:inline-block;
			vertical-align: middle;
			background:#00940e;
			color:#fff;
			font-size:14px;
			padding:2px 8px;
			border-radius: 99px;
		}


		.pd{
			padding:0 0 6px 0;
		}







	</style>
	<style>
		.navItem{
			display:inline-block;
			width:45%;
			max-width:300px;
			vertical-align: top;
			background:#eee;
			border-bottom: 5px solid #ccc;
			box-shadow: 2px 2px 3px #ddd;
			color:#666;
			text-decoration:none;
			border-radius: 8px;
			padding: 0 5px 3px;
		}
		.navItem.slc{
			border-bottom: 5px solid #00940e;
			color:#f60;
		}
		.navItem:hover{
			color:#d44;
		}
		.navTitle{
			text-align: center;
			font-size:18px;
			font-weight: bold;
		}
		.navItem.slc .navDesc{
			color:#00940e;
		}
		.navDesc{
			font-size:12px;
		}
	</style>



	<title>RecordApp测试</title>

</head>

<body>
<div class="main">


	<div class="mainBox">
		<div class="pd">
			<button  class="mainBtn"onclick="recreq()">请求权限</button>

			<button class="mainBtn" onclick="recstart()">录制</button>
			<button class="mainBtn" onclick="recstop()" style="margin-right:80px;">停止</button>

			<button onclick="recstopX()" style="margin-right:40px;">停止(仅清理)</button>

			<span style="display: inline-block;">
			<button onclick="recstartAndAutoStop()">录制+定时停止</button><input value="0" class="autoStopTime" style="width:60px;">ms
		</span>
		</div>
		<div class="pd recpower">
			<div style="height:40px;width:300px;background:#999;position:relative;">
				<div class="recpowerx" style="height:40px;background:#0B1;position:absolute;"></div>
				<div class="recpowert" style="padding-left:50px; line-height:40px; position: relative;"></div>
			</div>
		</div>
		<div class="pd">
			<div style="border:1px solid #ccc;display:inline-block"><div style="height:100px;width:300px;" class="recwave"></div></div>
		</div>
	</div>


	<div class="mainBox">
		<audio class="recPlay" style="width:100%"></audio>
		<div class="reclog"></div>
	</div>



	<div class="mainBox">
		<span class="lb">浏览器环境情况 :</span>
		<pre class="recinfoCode">
IsWx:${!!RecordApp.IsWx}
IsApp:${isApp}
Platforms:${RecordApp.Current.Key}

AudioContext:${!!window.AudioContext}
webkitAudioContext:${!!window.webkitAudioContext}
mediaDevices:${!!navigator.mediaDevices}
mediaDevices.getUserMedia:${!!(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia)}
navigator.getUserMedia:${!!navigator.getUserMedia}
navigator.webkitGetUserMedia:${!!navigator.webkitGetUserMedia}

URL:${location.href.replace(/#.+/g,"")}
UA:${navigator.userAgent}

RecordApp库修改时间（有可能修改了忘改）:${RecordApp.LM}
Recorder库修改时间（有可能修改了忘改）:${Recorder.LM}
</pre>
	</div>




</div>
</body>


<!-- 加载独立配置文件，免得修改app.js -->
<script
		src="ios-weixin-config.js"
></script>
<!-- 加载app.js -->
<script
		src="app.js"
>


</script>

<script
		src="ztest-jquery.min-1.9.1.js"
></script>

<script>
    function RandomKey(){
        return "randomkey"+(RandomKey.idx++);
    };
    RandomKey.idx=0;
</script>


<script>
    (function(){
//注册可选扩展库
        var paths=RecordApp.Platforms.Default.Config.paths;
        paths.push({
            url:RecordAppBaseFolder+"waveview.js"
            ,lazyBeforeStart:1 //开启延迟加载，在Start调用前任何时间进行加载都行
            ,check:function(){return !Recorder.WaveView}
        });

//可以设置组件是否进行延迟加载，默认会延迟加载，不会阻塞Install
        RecordApp.UseLazyLoad=!(+localStorage["RecordApp_UseLazyLoadDisable"]||0);

//立即加载环境，自动把Recorder加载进来
        RecordApp.Install(function(){
            console.log("RecordApp.Install成功");
            isInstall=true;
            window.onInstall&&onInstall();
        },function(err){
            var msg="RecordApp.Install出错："+err;
            console.log(msg);
            alert(msg);
        });

    })();
    var isInstall=false;
</script>
<script>
    //后期改成console.log
    function reclog(s,color){
        var now=new Date();
        var t=("0"+now.getHours()).substr(-2)
            +":"+("0"+now.getMinutes()).substr(-2)
            +":"+("0"+now.getSeconds()).substr(-2);
        $(".reclog").prepend('<div style="color:'+(!color?"":color==1?"red":color==2?"#0b1":color)+'">['+t+']'+s+'</div>');
    };
    window.onerror=function(message, url, lineNo, columnNo, error){
        reclog('<span style="color:red">【Uncaught Error】'+message+'<pre>'+"at:"+lineNo+":"+columnNo+" url:"+url+"\n"+(error&&error.stack||"不能获得错误堆栈")+'</pre></span>');
    };
</script>



<script>
    function baseSet(){
        var alwaysUseWeixinJS=false;//根据需要修改
        if(!!RecordApp.alwaysUseWeixinJSPrev!=alwaysUseWeixinJS){
            reclog("JsSDK选项变更，已重置RecordApp，请先进行权限测试");
            RecordApp.Current=null;
            RecordApp.alwaysUseWeixinJSPrev=alwaysUseWeixinJS;
        };
        RecordApp.AlwaysUseWeixinJS=alwaysUseWeixinJS;

        cancelAutoStop();
    };
    function recreq(){
        baseSet();
        reclog("开始请求授权...");


        dialogInt=setTimeout(function(){//定时8秒后打开弹窗，主要用于监测浏览器没有发起权限请求的情况，在RequestPermission前放置定时器利于收到了回调能及时取消（不管open是同步还是异步回调的）
            showDialog();
        },8000);

        RecordApp.RequestPermission(function(){
            dialogCancel();
            reclog(RecordApp.Current.Key+"已授权",2);
        },function(err,isUserNotAllow){
            dialogCancel();
            reclog(RecordApp.Current.Key+(isUserNotAllow?" UserNotAllow，":"")+" 授权失败："+err,1);
        });

        window.waitDialogClick=function(){
            dialogCancel();
            reclog("打开失败：权限请求被忽略，<span style='color:#f00'>用户主动点击的弹窗</span>");
        };
    };

    //我们可以选择性的弹一个对话框：为了防止移动端浏览器存在第三种情况：用户忽略，并且（或者国产系统UC系）浏览器没有任何回调
    var showDialog=function(){
        if(!/mobile/i.test(navigator.userAgent)){
            return;//只在移动端开启没有权限请求的检测
        };
        dialogCancel();
        $("body").append(''
            +'<div class="waitDialog" style="z-index:99999;width:100%;height:100%;top:0;left:0;position:fixed;background:rgba(0,0,0,0.3);">'
            +'<div style="display:flex;height:100%;align-items:center;">'
            +'<div style="flex:1;"></div>'
            +'<div style="width:240px;background:#fff;padding:15px 20px;border-radius: 10px;">'
            +'<div style="padding-bottom:10px;">录音功能需要麦克风权限，请允许；如果未看到任何请求，请点击忽略~</div>'
            +'<div style="text-align:center;"><a onclick="waitDialogClick()" style="color:#0B1">忽略</a></div>'
            +'</div>'
            +'<div style="flex:1;"></div>'
            +'</div>'
            +'</div>');
    };
    var dialogInt;
    var dialogCancel=function(){
        clearTimeout(dialogInt);
        $(".waitDialog").remove();
    };
    //弹框End


    var curSet,autoStopTimer;
    function recstartAndAutoStop(){
        var time=+$(".autoStopTime").val()||0;
        if(time<100){
            reclog("定时不能小于100ms",1);
            return;
        };
        recstart(function(msg){
            if(msg){
                msg&&reclog(msg,1);
                return;
            };
            reclog("定时"+time+"ms后自动停止录音");
            autoStopTimer=setTimeout(function(){
                autoStopTimer=0;
                reclog("定时时间到，开始自动调用停止...");
                recstop();
            },time);
        });
    };
    var cancelAutoStop=function(){
        if(autoStopTimer){
            reclog("已取消定时停止",1);
            clearTimeout(autoStopTimer);
            autoStopTimer=0;
        };
    };

    /**
     * 开始录制
     * @param call
     */
    function recstart(call){
        call||(call=function(msg){
            msg&&reclog(msg,1);
        });
        baseSet();
        if(!RecordApp.Current){
            call("需先调用RequestPermission");
            return;
        };
        if(RecordApp.Current==RecordApp.Platforms.Weixin){
            reclog("正在使用微信JsSDK，录音过程中不会有任何回调");
        }else{
            reclog("正在使用H5录音，底层由Recorder直接提供支持");
        };

        var type="mp3";
        var bit="16";
        var sample="16000";
        window.waveView={};
        var set={
            type:type
            ,bitRate:bit
            ,sampleRate:sample
            ,onProcess:function(buffers,powerLevel,duration,sampleRate){
                console.log("执行onProcess")
                $(".recpowerx").css("width",powerLevel+"%");
                $(".recpowert").text(formatMs(duration,1)+" / "+powerLevel);

                if(waveView&&waveView.input)
                    waveView.input(buffers[buffers.length-1],powerLevel,sampleRate);
            }
        };
        curSet=null;
        reclog(RecordApp.Current.Key+"正在打开...");
        RecordApp.Start(set,function(){
            curSet=set;
            reclog(RecordApp.Current.Key+"已打开:"+type+" "+bit+"kbps",2);
            waveView=Recorder.WaveView({elem:".recwave"});
            call();
        },function(err){
            call(RecordApp.Current.Key+"打开失败："+err);
        });
    };
    function recstopX(){
        cancelAutoStop();
        RecordApp.Stop(
            null //success传null就只会清理资源，不会进行转码
            ,function(msg){
                reclog("已清理，错误信息："+msg);
            }
        );
    };
    var recblob={};
    function recstop(call){
        var set=curSet;
        recstopFn(call,true,function(err,blob,time){

        });
    };
    function recstopFn(call,isClick,endCall,rec){
        cancelAutoStop();
        call||(call=function(msg){
            msg&&reclog(msg,1);
        });

        var t1=Date.now();
        if(!isClick){
            rec.stop(function(blob,time){
                var tag=endCall("",blob,time);
                if(tag==-1){
                    return;
                };

                addRecLog(time,tag||"已录制",blob,rec.set,t1);
            },function(s){
                reclog("失败："+s);
                endCall(s);
            });
            return;
        };

        var setData=curSet;
        curSet=null;

        if(!setData){
            //没有开始？不管，stop清理资源
            setData={};
        }else{
            reclog(RecordApp.Current.Key+"正在结束"+setData.type+"...");
        };

        RecordApp.Stop(function(blob,time){
            endCall("",blob,time);

            var wxData=setData.DownWxMediaData;//微信JsSDK 下载到的音频源文件
            if(wxData){
                var list=wxData.list;
                delete wxData.list;

                reclog("<span style='color:#0b1'>发现微信JsSDK录制的源文件，共"+list.length+"段，时间统计："+JSON.stringify(wxData)+"</span>");
                if(!Recorder.AMR){
                    reclog("<span style='color:#fb0'>播放需要上面点击换到amr类型，加载amr解码器，目前未加载<span>");
                };

                var t1x=t1;
                t1=Date.now();
                for(var i=0;i<list.length;i++){
                    var obj=list[i];
                    var bstr=atob(obj.data),n=bstr.length,u8arr=new Uint8Array(n);
                    while(n--){
                        u8arr[n]=bstr.charCodeAt(n);
                    };
                    var blob2=new Blob([u8arr.buffer], {type:obj.mime});
                    addRecLog(obj.duration,"<span style='color:#0b1'>微信源片段"+(i+1)+"</span>",blob2,{type:/\/(\w+)/.exec(obj.mime)[1]},t1);
                };
                t1=t1x;
            };
            addRecLog(time,"已录制",blob,setData,t1);
            call(null,{data:blob,duration:time});
        },function(s){
            endCall(s);
            call("失败："+s);
        });
    };
    var addRecLog=function(time,tag,blob,set,t1){
        var id=RandomKey(16);
        recblob[id]={blob:blob,set:$.extend({},set),time:time};
        reclog(tag+":"+intp(set.bitRate,3)+"kbps "+intp(set.sampleRate,5)+"hz 花"+intp(Date.now()-t1,4)+"ms编码"+intp(blob.size,6)+"b ["+set.type+"]"+formatMs(time)+'ms <button onclick="recdown(\''+id+'\')">下载</button> <button onclick="recplay(\''+id+'\')">播放</button> <span class="p'+id+'"></span> <span class="d'+id+'"></span>');
    };
    var intp=function(s,len){
        s=s==null?"-":s+"";
        if(s.length>=len)return s;
        return ("_______"+s).substr(-len);
    };
    var formatMs=function(ms,all){
        var f=Math.floor(ms/60000),m=Math.floor(ms/1000)%60;
        var s=(all||f>0?(f<10?"0":"")+f+":":"")
            +(all||f>0||m>0?("0"+m).substr(-2)+"″":"")
            +("00"+ms%1000).substr(-3);
        return s;
    };
    function recplay(key){
        var o=recblob[key];
        if(o){
            var audio=$(".recPlay")[0];
            audio.controls=true;
            if(!(audio.ended || audio.paused)){
                audio.pause();
            };
            o.play=(o.play||0)+1;
            var logmsg=function(msg){
                $(".p"+key).html('<span style="color:green">'+o.play+'</span> '+new Date().toLocaleTimeString()+" "+msg);
            };
            logmsg("");
            audio.onerror=function(e){
                logmsg('<span style="color:red">播放失败['+audio.error.code+']'+audio.error.message+'</span>');
            };

            var end=function(blob){
                audio.src=(window.URL||webkitURL).createObjectURL(blob);
                audio.play();
            };
            var wav=Recorder[o.set.type+"2wav"];
            if(wav){
                logmsg("正在转码成wav...");
                wav(o.blob,function(blob){
                    end(blob);
                    logmsg("已转码成wav播放");
                },function(msg){
                    logmsg("转码成wav失败："+msg);
                });
            }else{
                end(o.blob);
            };
        };
    };
    function recdown(key){
        var o=recblob[key];
        if(o){
            var cls=RandomKey(16);
            var name="rec-"+o.time+"ms-"+o.set.bitRate+"kbps-"+o.set.sampleRate+"hz."+o.set.type;

            o.down=(o.down||0)+1;
            $(".d"+key).html('<span style="color:red">'+o.down+'</span> 点击 <span class="'+cls+'"> 下载，或复制文本<button onclick="recdown64(\''+key+'\',\''+cls+'\')">生成Base64文本</button></span>');

            var downA=document.createElement("A");
            downA.innerHTML="下载 "+name;
            downA.href=(window.URL||webkitURL).createObjectURL(o.blob);
            downA.download=name;
            $("."+cls).prepend(downA);
            //downA.click(); 某些软件内会跳转页面到恶心推广页
        };
    };
    function recdown64(key, cls){
        var o=recblob[key];

        var reader = new FileReader();
        reader.onloadend = function() {
            var id=RandomKey(16);
            $("."+cls).append('<textarea class="'+id+'"></textarea>');
            $("."+id).val(reader.result);
        };
        reader.readAsDataURL(o.blob);
    };
    reclog("点击录制开始哦");

</script>










<script>
    if(/mobile/i.test(navigator.userAgent)){
        //移动端加载控制台组件
        var elem=document.createElement("script");
        elem.setAttribute("type","text/javascript");
        elem.setAttribute("src","https://cdn.bootcss.com/eruda/1.5.4/eruda.min.js");
        $("head")[0].appendChild(elem);
        elem.onload=function(){
            eruda.init();
        };
    };
</script>
<div style="padding:100px;"></div>









<script>
    function loadEngine(input){
        if(input.length&&input[0].nodeName=="INPUT"){
            var type=input.val();
            var engines=input.attr("engine").split(",");
            var end=function(){
                var enc=Recorder.prototype["enc_"+type];
                var tips=[!enc?"这个编码器无提示信息":type+"编码器"+(enc.stable?"稳定版":"beta版")+"，<span style='color:"+(type=="wav"?"#0b1'>wav转码超快":Recorder.prototype[type+"_start"]?"#0b1'>支持边录边转码(Worker)":"red'>仅支持标准UI线程转码")+"</span>，"+enc.testmsg];
                tips.push('<div style="color:green">');
                tips.push("【使用"+type+"录音需要在app.js Platforms.Default内注册】：");
                tips.push("src/recorder-core.js, src/engine/");
                tips.push(engines.join(".js, src/engine/"));
                tips.push(".js</div>");

                $(".typeTips").html(tips.join(""));
            };
            if(!Recorder.prototype[type]){
                reclog("<span style='color:#f60'>正在加载"+type+"编码器，请勿操作...</span>");
                var i=-1;
                var load=function(){
                    i++;
                    if(i>=engines.length){
                        reclog("<span style='color:#0b1'>"+type+"编码器已加载，可以录音了</span>");
                        end();
                        return;
                    }
                    var elem=document.createElement("script");
                    elem.setAttribute("type","text/javascript");
                    elem.setAttribute("src",RecordAppBaseFolder+engines[i]+".js");
                    elem.onload=function(){
                        load();
                    };
                    $("head")[0].appendChild(elem);
                };
                load();
            }else{
                end();
            };
        };
    };


    function onInstall(){
        var isApp;
        RecordApp.Platforms.Native.Config.IsApp(function(v){isApp=v;});

        $(".recinfoCode").text($(".recinfoCode").text().replace(/\$\{(.+?)\}/g,function(a,b){return eval(b)}));


        reclog("提示：部分组件可能会设置在Install成功后进行延迟加载，Start、Stop操作在组件未加载完时会等待OnLazyReady事件触发，可通过设置RecordApp.UseLazyLoad=false关闭此特性（会阻塞Install导致RequestPermission变慢），<span style='color:"+(RecordApp.UseLazyLoad?"#0b1'>当前已启用":"red'>当前已关闭")+"此特性</span><button onclick='changeUseLazyLoad()'>"+(RecordApp.UseLazyLoad?"关闭":"启用")+"</button>");
        window.changeUseLazyLoad=function(){
            localStorage["RecordApp_UseLazyLoadDisable"]=RecordApp.UseLazyLoad?1:0;
            location.reload();
        };
        RecordApp.Current.OnLazyReady(function(){
            reclog("OnLazyReady触发，所有组件已完成加载",2);
            loadEngine("mp3");
        });
        reclog("Install成功，环境："+RecordApp.Current.Key+"，可以录音了",2);
    };
    reclog("<span style='color:#f60'>正在执行Install，请勿操作...</span>");
    if(isInstall){
        onInstall();
    };

</script>



</html>